package IP地址的获取输出;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Test {
    public static void main(String[] args) {
        try {
            InetAddress inet1=InetAddress.getByName("www.baidu.com");//百度域名地址
            System.out.println("（1）百度域名/IP地址："+inet1);
            System.out.println("--------------------------");

            InetAddress inet2=InetAddress.getByName("220.181.38.149");//百度IP地址
            System.out.println("（2）百度IP地址："+inet2);
            System.out.println("--------------------------");

            InetAddress inet3 = InetAddress.getByName("localhost");//127.0.0.1是本地电脑的IP地址，localhost是本地电脑的域名地址
            System.out.println("本地电脑域名/以及默认本地IP地址："+inet3);
            System.out.println("--------------------------");



            //获取本地ip
            InetAddress inet4 = InetAddress.getLocalHost();//获取本地电脑的局域网IP地址/域名地址
            System.out.println("本地电脑域名/IP地址(在局域网中的IP地址)："+inet4);
            System.out.println("--------------------------");

            //getHostName()
            System.out.println("本地电脑域名(在局域网中的IP地址)："+inet4.getHostName());//获取某个主机的域名地址
            //getHostAddress()
            System.out.println("本地电脑IP地址(在局域网中的IP地址)："+inet4.getHostAddress());//获取某个主机的IP地址

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
